package cn.zr.aitool.db;

import android.content.ContentValues;
import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

import cn.zr.aitool.bean.ContentBean;

/**
 * Description: GroupCursorManager.java Create on 2013-5-14 上午10:27:25
 *
 * @author wk
 * @version 1.0 Copyright (c) 2013 Company,Inc. All Rights Reserved.
 */
public class ContentManager extends DbBaseManager implements ContentColumn {

    private static ContentManager mManager;

    private ContentManager() {
        super(DataBaseHelper.TALE_CONTENT);
    }

    public static ContentManager getInstance() {
        if (mManager == null) {
            mManager = new ContentManager();
        }
        return mManager;
    }

    public List<ContentBean> getAllContent() {
        Cursor cursor = queryAll(mContext);
        return cursor2Group(cursor);
    }

    public long insertGroup(ContentValues contentValues) {
        return insertSigle(mContext, contentValues);
    }

    public List<ContentBean> queryContentByPosition(int tPosition) {
        List<ContentBean> texts = queryGroupsBy1Selection(position, String.valueOf(tPosition));
        return texts;
    }

    public List<ContentBean> queryContentsByTitle(String values) {
        return queryGroupsBy1Selection(title, values);
    }

    public List<ContentBean> queryGroupsBy1Selection(String selectionRow, String values) {
        return queryGroupsByTwoSelection(new String[]{selectionRow}, new String[]{values});
    }

    public List<ContentBean> queryGroupsByTwoSelection(String[] selectionRow, String[] selectionArgs) {
        Cursor cursor = queryByTwoSelecionByAnd(mContext, selectionRow, selectionArgs, null, null);
        return cursor2Group(cursor);
    }

    public void clearAllData() {
        delete(mContext, "", null);
    }

    public void deleteContentPosition(int mPosition) {
        String selection = position + "=?";
        String[] selectionArgs = new String[]{String.valueOf(mPosition)};
        delete(mContext, selection, selectionArgs);
    }

    public void deleteById(int index) {
        String selection = ID + "=?";
        String[] selectionArgs = new String[]{String.valueOf(index)};
        delete(mContext, selection, selectionArgs);
    }

    public void insertContent(List<ContentBean> groups) {
        List<ContentValues> values = new ArrayList<ContentValues>();

        for (ContentBean group : groups) {
            values.add(group.toContentValues());
        }
        insertAll(mContext, values);
    }

    public List<ContentBean> cursor2Group(Cursor cursor) {
        List<ContentBean> groups = new ArrayList<ContentBean>();
        if (cursor == null) {
            return groups;
        }

        int idIndex = cursor.getColumnIndex(ID);
        int titleIndex = cursor.getColumnIndex(title);
        int contentIndex = cursor.getColumnIndex(content);
        int startIndex = cursor.getColumnIndex(start);
        int endIndex = cursor.getColumnIndex(end);
        int positionIndex = cursor.getColumnIndex(position);
        int contentOneIndex = cursor.getColumnIndex(content_one);
        int contentTwoIndex = cursor.getColumnIndex(content_two);
        int contentThrIndex = cursor.getColumnIndex(content_thr);
        int typeIndex = cursor.getColumnIndex(type);

        ContentBean group;
        while (cursor.moveToNext()) {
            group = new ContentBean();
            group.id = cursor.getInt(idIndex);
            group.content = cursor.getString(contentIndex);
            group.title = cursor.getString(titleIndex);
            group.start = cursor.getString(startIndex);
            group.end = cursor.getString(endIndex);
            group.position = cursor.getInt(positionIndex);
            group.type = cursor.getInt(typeIndex);
            group.content1 = cursor.getString(contentOneIndex);
            group.content2 = cursor.getString(contentTwoIndex);
            group.content3 = cursor.getString(contentThrIndex);
            groups.add(group);
        }
        closeCursor(cursor);
        return groups;
    }

}
